package com.franklin.java.api.psi.data.resolver;

import com.franklin.java.api.psi.data.PsiData;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiLambdaExpression;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiType;
import com.intellij.psi.util.PsiTreeUtil;
import org.apache.commons.lang3.ArrayUtils;

import java.util.Objects;
import java.util.Optional;

/**
 * 解析Lambda表达式
 * @author Franklin
 * @since 2021/09/04 22:24
 */
public class LambdaExpressionResolver implements PsiDataResolver {
    @Override
    public PsiData resolve(String name, String value, PsiElement dataElement) {
        PsiLambdaExpression psiLambdaExpression = PsiTreeUtil.getParentOfType(dataElement, PsiLambdaExpression.class);
        if (Objects.isNull(psiLambdaExpression)){
            return null;
        }
        PsiParameter[] psiParameters = psiLambdaExpression.getParameterList().getParameters();
        for (PsiParameter psiParameter : psiParameters) {
            if (psiParameter.getName().equals(name)){
                PsiType psiType = psiParameter.getType();
                String fullName = psiType.getCanonicalText();
                String shortName = psiType.getPresentableText();
                return new PsiData(shortName,name,psiParameter,fullName,dataElement);
            }
        }
        return null;
    }
}
